package adv10;

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class AddProduct {
	// 增加新品
	public static void main(String[] args) throws Exception {
		SAXReader re = new SAXReader();
		Document doc = re.read(new File("products.xml"));
		Scanner in = new Scanner(System.in);
		System.out.print("请输入商品号：");
		String itemno = in.next();
		System.out.println("name:");
		String name = in.next();
		System.out.print("请输入商品其他信息：");
		
		double price = in.nextDouble();
		int shuliang = in.nextInt();
		boolean zhekou = in.nextBoolean();
		String changjia = in.next();
		String unit = in.next();
		Element root=doc.getRootElement();
		
		//创建爱你product子节点
		Element p=root.addElement("product");
		//给product添加两个属性
		p.addAttribute("discount", zhekou+"");
		p.addAttribute("factoryaddr", changjia);
		//product的子节点
		p.addElement("itemno").setText(itemno);
		p.addElement("name").setText(name);
		p.addElement("price").setText(Double.toString(price));
		p.addElement("stockcount").setText(Integer.toString(shuliang));
		p.addElement("unit").setText(unit);
		
		OutputFormat f = OutputFormat.createPrettyPrint();
		f.setEncoding("utf-8");
		XMLWriter xm = new XMLWriter(new FileWriter("products.xml"), f);
		xm.write(doc);
		xm.close();
	}

}
